//
//  ListHotNewsViewController.m
//  Doc Bao
//
//  Created by sieg on 10/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ListHotNewsViewController.h"
#import "News.h"
#import "NewsExtensions.h"
#import "Post.h"
#import "PostExtensions.h"
#import "PostCell.h"
#import "FullPostViewController.h"

@implementation ListHotNewsViewController

@synthesize newsTable;
@synthesize listNews;
@synthesize headerSection;
@synthesize imageView;
@synthesize headerName;
@synthesize postCell;

#pragma mark -
#pragma mark UITableView Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return [listNews count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	NSArray *listPostOfNew = [Post getPostsWithNews:[[[listNews objectAtIndex:section] Id] intValue]];
	return [listPostOfNew count] > 0 ? [listPostOfNew count] : 1;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {	
	return 50.0f;	
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return 77.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  
  News *aNew = [listNews objectAtIndex:section];
  
  headerName.text = [aNew Name];
  imageView.image = [UIImage imageNamed:[aNew Logo]];  
	return headerSection;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	static NSString *newsCellId = @"NewsCellId";
	postCell = (PostCell*)[newsTable dequeueReusableCellWithIdentifier:newsCellId];
	if (postCell == nil) {
		[[NSBundle mainBundle] loadNibNamed:@"PostCell" owner:self options:nil];
	}
  
  NSArray *postWithNewIdArray = [Post getPostsWithNews:[[[listNews objectAtIndex:indexPath.section] Id] intValue]];
  if ([postWithNewIdArray count] > 0) {
	  Post *aPost = [postWithNewIdArray objectAtIndex:indexPath.row];
	  postCell.thumbnailImage.image = [UIImage imageWithData:aPost.Thumbnail];
	  postCell.titleLabel.text = aPost.Title;
	  postCell.descriptionTextView.text = aPost.Description;
  }
  else {
	  postCell.titleLabel.text = @"Khong co news moi";
	  postCell.descriptionTextView.text = @" ";
  }

	return [postCell autorelease];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	
	NSArray *postWithNewIdArray = [Post getPostsWithNews:[[[listNews objectAtIndex:indexPath.section] Id] intValue]];
	if ([postWithNewIdArray count] > 0) {
		Post *aPost = [postWithNewIdArray objectAtIndex:indexPath.row];
		FullPostViewController *controller = [[FullPostViewController alloc] init];
		[self.navigationController pushViewController:controller animated:YES];
		[controller loadNewsWithID:aPost.NewsId withLink:aPost.Link];
		[controller release];
	}
}

#pragma mark -

  // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[super viewDidLoad];
	listNews = [[NSMutableArray alloc] init];
	[listNews setArray:[News getNews]];
	[newsTable reloadData];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
  [super didReceiveMemoryWarning];
  
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
  [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	self.newsTable = nil;
	self.postCell = nil;
}

- (void)dealloc {
	[newsTable release];
  [imageView release];
	[headerName release];
  [headerSection release];
	[listNews release];
	[postCell release];
  [super dealloc];
}


@end